package mn.idax.exchange.otc.dto;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import mn.idax.exchange.common.UTCTimeUtil;
import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.entity.ChatLog;
import mn.idax.exchange.otc.entity.UserInfo;
import mn.idax.exchange.otc.enums.LogTypeEnum;
import mn.idax.exchange.otc.enums.MessageTypeEnum;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatProtocol {

    private static final String SYSTEM_SENDER_NAME = "system";

    private Integer senderId;
    private String userName;
    private String userLogo;
    private String message;
    private String source;
    private String name;
    private String orderId;
    private MessageTypeEnum msgType;
    private LogTypeEnum logType;
    private Integer readFlag;

    private Integer buyerReadFlag;
    private Integer sellerReadFlag;
    private Integer buyerId;
    private Integer sellerId;
    private String orderNum;

    private long sendTime = UTCTimeUtil.getUTCTimeLong();
    private String uuid = UUID.randomUUID().toString();
    private boolean saved = false;

    @JSONField(serialize = false)
    private List<String> users = new ArrayList<>();
    @JSONField(serialize = false)
    private List<String> rooms = new ArrayList<>();

    private Object[] dynamicParam;


    public ChatProtocol(Integer senderId, String userName, String userLogo, String message, String source, String name, String orderId, MessageTypeEnum msgType, LogTypeEnum logType) {
        this.senderId = senderId;
        this.userName = userName;
        this.userLogo = userLogo;
        this.message = message;
        this.source = source;
        this.name = name;
        this.orderId = orderId;
        this.msgType = msgType;
        this.logType = logType;
    }

    public ChatProtocol(Integer senderId, String userName, String userLogo, String message, String source, String name, String orderId, MessageTypeEnum msgType, LogTypeEnum logType, Collection<String> users, Collection<String> rooms) {
        this(senderId, userName, userLogo, message, source, name, orderId, msgType, logType);
        this.users.addAll(users);
        this.rooms.addAll(rooms);
    }

    public ChatProtocol(Collection<String> users, Collection<String> rooms) {
        this.users.addAll(users);
        this.rooms.addAll(rooms);
    }

    public static ChatProtocol defalutSystemMessage(String message) {
        ChatProtocol protocol = new ChatProtocol();
        protocol.userName = SYSTEM_SENDER_NAME;
        protocol.message = message;
        protocol.msgType = MessageTypeEnum.text;
        protocol.logType = LogTypeEnum.system;
        return protocol;
    }

    public static ChatProtocol chatLogToChatProtocol(ChatLog chatLog) {
        if (chatLog == null) {
            return null;
        }
        ChatProtocol chatProtocol = new ChatProtocol();
        BeanUtils.copyProperties(chatLog, chatProtocol);
        return chatProtocol;
    }

    /**
     * addExtraInfo
     *
     * @param chatProtocol
     * @param userInfo
     * @param logType
     * @return
     */
    public static ChatProtocol addExtraInfo(ChatProtocol chatProtocol, UserInfo userInfo, LogTypeEnum logType) {
        if (chatProtocol == null || userInfo == null) {
            return null;
        }
        chatProtocol.setSenderId(userInfo.getId());
        chatProtocol.setUserName(userInfo.getUserName());
        chatProtocol.setLogType(logType);
        if (logType.equals(LogTypeEnum.staff)){
            chatProtocol.setUserLogo(CommonConstant.STUFF_AVATAR);
        } else {
            chatProtocol.setUserLogo(userInfo.getUserLogo());
        }

        return chatProtocol;
    }

    public Integer getSenderId() {
        return senderId;
    }

    public void setSenderId(Integer senderId) {
        this.senderId = senderId;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MessageTypeEnum getMsgType() {
        return msgType;
    }

    public void setMsgType(MessageTypeEnum msgType) {
        this.msgType = msgType;
    }

    public Integer getReadFlag() {
        return readFlag;
    }

    public void setReadFlag(Integer readFlag) {
        this.readFlag = readFlag;
    }

    public long getSendTime() {
        return sendTime;
    }

    public void setSendTime(long sendTime) {
        this.sendTime = sendTime;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<String> getUsers() {
        return users;
    }

    public void setUsers(Collection<String> users) {
        this.users.addAll(users);
    }

    public void setUsers(List<String> users) {
        this.users = users;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public List<String> getRooms() {
        return rooms;
    }

    public void setRooms(String room) {
        List<String> roomList = new ArrayList<>();
        roomList.add(room);
        this.setRooms(roomList);
    }

    public void setRooms(List<String> rooms) {
        this.rooms = rooms;
    }

    public void setRooms(Collection<String> rooms) {
        this.rooms.addAll(rooms);
    }

    public LogTypeEnum getLogType() {
        return logType;
    }

    public void setLogType(LogTypeEnum logType) {
        this.logType = logType;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserLogo() {
        return userLogo;
    }

    public void setUserLogo(String userLogo) {
        this.userLogo = userLogo;
    }


    public boolean isSaved() {
        return saved;
    }

    public void setSaved(boolean saved) {
        this.saved = saved;
    }
}
